package com.xianniu.ltc.config;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * 冻结表头
 * @Author: dingyx
 * @Date: 2021/12/6 2:25 下午
 */
public class FreezeAndFilter implements SheetWriteHandler {
    /**
     * 要冻结的列数
     */
    public int colSplit = 1;
    /**
     * 要冻结的行数
     */
    public int rowSplit = 1;
    /**
     * 标示被固定列右边的第一列的列号
     */
    public int leftmostColumn = 1;
    /**
     * 标示被固定列右边的第一列的列号
     */
    public int topRow = 1;
    public String autoFilterRange = "1:1";

    public FreezeAndFilter(){}

    public FreezeAndFilter(int colSplit, int rowSplit, int leftmostColumn, int topRow){
        this.colSplit = colSplit;
        this.rowSplit = rowSplit;
        this.leftmostColumn = leftmostColumn;
        this.topRow = topRow;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        sheet.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
        sheet.setAutoFilter(CellRangeAddress.valueOf(autoFilterRange));
    }
}
